package repository

import (
	"gorm.io/gorm"
	"guliedu/model"
	"guliedu/utils"
	"mime/multipart"
)

type IEduVideoRepository interface {
	// 添加视频
	Insert(video *model.EduVideo) error
	// 上传视频
	UploadVideo(file multipart.File, fileSize int64) (string, int)
	// 查询视频
	Select(id string) (*model.EduVideo, error)
	// 更新视频
	Update(video *model.EduVideo) error
	// 删除视频
	Delete(id string) error
	// 根据章节查询视频
	SelectByChapterID(id string) ([]model.EduVideo, error)
	// 根据课程ID删除视频
	DeleteByCourseID(id string) error
}

func NewIEduVideoRepository() IEduVideoRepository {
	return &VideoRepository{mysqlDb: db}
}

type VideoRepository struct {
	mysqlDb *gorm.DB
}

func (t *VideoRepository) Insert(video *model.EduVideo) error {
	return t.mysqlDb.Create(&video).Error
}

func (t *VideoRepository) UploadVideo(file multipart.File, fileSize int64) (string, int) {
	return utils.UploadFile(file, fileSize)
}

func (t *VideoRepository) Select(id string) (video *model.EduVideo, err error) {
	err = t.mysqlDb.Where("id = ?", id).Find(&video).Error
	return
}

func (t *VideoRepository) Update(video *model.EduVideo) error {
	return t.mysqlDb.Save(&video).Error
}

func (t *VideoRepository) Delete(id string) error {
	return t.mysqlDb.Where("id = ?", id).Delete(&model.EduVideo{}).Error
}

func (t *VideoRepository) SelectByChapterID(id string) (videos []model.EduVideo, err error) {
	err = t.mysqlDb.Where("chapter_id = ?", id).Find(&videos).Error
	return
}

func (t *VideoRepository) DeleteByCourseID(id string) error {
	return t.mysqlDb.Where("course_id = ?", id).Delete(&model.EduVideo{}).Error
}
